using UnityEngine;
using System.Collections;

public class Joystick_Mode : MonoBehaviour {
	
	public Transform stick;
	
	private GameManager GameManager_script;
	private bool isPress = false;
	Vector3 StickOriginPos;
	Vector3 touchPoint;
	
	void Start () {
		GameManager_script = GameManager.getInstance();
	}
	
	void Update () {
		if( isPress )
		{
			foreach ( Touch touch in Input.touches) {
				touchPoint.Set(touch.position.x,touch.position.y,0.0f);
				Vector3 dummy = touchPoint - StickOriginPos;
				
				if ( Vector3.Distance(Vector3.zero, (touchPoint - StickOriginPos) ) <80.0f)
				{
					stick.localPosition = dummy;
					dummy.Normalize();
					GameManager_script.Player_Direction.Set(dummy.x, 0.0f, dummy.y);
				}
				else
				{
					dummy.Normalize();
					stick.localPosition = dummy * 80.0f;
					GameManager_script.Player_Direction.Set(dummy.x, 0.0f, dummy.y);
				}
			}
		}
	}
	void JoyStickPress()
	{
		isPress = true;
		touchPoint.Set(Input.touches[0].position.x,Input.touches[0].position.y,0.0f);
		StickOriginPos = touchPoint;
	}
	void JoyStickRelease()
	{
		isPress = false;
		stick.localPosition = Vector3.zero;
		GameManager_script.Player_Direction = Vector3.zero;
		GameManager_script.PlayerAnimation.Play("idle");
	}
}
